package io.wdb.wxapp.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.wdb.common.exception.RRException;
import io.wdb.common.exception.RspCodeEnum;
import io.wdb.common.utils.SignUtil;
import io.wdb.handler.ApiServiceHandler;
import io.wdb.modules.activity.wxapp.entity.HdConfigEntity;
import io.wdb.modules.activity.wxapp.service.HdCardInitiatorService;
import io.wdb.modules.activity.wxapp.service.HdConfigService;
public class BaseController {
	private static Logger logger = LoggerFactory.getLogger(BaseController.class);
	@Value(value = "${wxapp.appid}")
	public String appid;
	@Value(value = "${wxapp.secret}")
	public String secret;
	
	@Value(value = "${wxapp.token}")
	public String token;
	
	@Value(value = "${wxapp.merchant_id}")
	public String merchantId;
	
	@Value(value = "${wxapp.merchant_key}")
	public String merchantKey;
	
	@Value(value = "${wxapp.notify_url}")
	public String notifyUrl;
	
	@Autowired
	private HdConfigService hdConfigService;
	
	@Autowired
	ApiServiceHandler apiServiceHandler;
	
	//获取配置文件
	public String getConfig(String configKey) {
		HdConfigEntity entity = hdConfigService.queryObject(configKey);
		return entity.getHdValue();
	}
	
	public StringBuffer getParams(HttpServletRequest request) {
		Enumeration enu=request.getParameterNames();
		StringBuffer result = new StringBuffer();
		while (enu.hasMoreElements()) {
			String paraName = (String) enu.nextElement();
			result.append(paraName + ": " + request.getParameter(paraName)+",");
		}
		return result;
	}
	
	/**
	 * 微信消息验证接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "checksign", method = RequestMethod.POST)
	@ResponseBody
	public Object index(HttpServletRequest request) {
		// 微信加密签名
		String signature = request.getParameter("signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");
		logger.info("微信校验:{} {} {} {}",signature, timestamp, nonce, echostr);
		if(echostr != null){
			return echostr;
		}
		if (SignUtil.checkSignature(token, signature, timestamp, nonce)) {
			logger.info("微信check true");
		}
		return echostr;
	}
	
	protected void checkIsNull(Object obj, String key) throws RRException {
		if(ObjectUtils.isEmpty(obj) || "undefined".equals(obj) || "null".equals(obj)) {
			throw new RRException(RspCodeEnum.PARAM_CHECK_IS_NULL.setRspMessage(key));
		}
	}
	
	public boolean isEmpty(Object obj) {
		if(ObjectUtils.isEmpty(obj) || "undefined".equals(obj) || "null".equals(obj)) {
			return true;
		}
		return false;
	}
}
